package com.mathdroid.gui.main.talents;

import java.util.LinkedList;

import javax.swing.JTabbedPane;

import com.mathdroid.model.EModelObserverEvent;
import com.mathdroid.model.IModelObserver;
import com.mathdroid.model.Model;
import com.mathdroid.model.enums.ESpec;
import com.mathdroid.model.enums.ETalentTree;
import com.mathdroid.model.talents.Specializations;

/**
 * This TabbedPane contains the three talent trees. Each tree has 28 buttons,
 * which can be hidden, if unnecessary.
 */
public class TalentTreeTabbedPane 
	extends JTabbedPane implements IModelObserver {
	/**
	 * Default UID.
	 */
	private static final long serialVersionUID = 1L;
	
	/**
	 * List of the three talent trees UIs.
	 */
	private LinkedList<TalentTree> trees = new LinkedList<TalentTree>();
	
	/**
	 * Standard constructor.
	 */
	public TalentTreeTabbedPane() {
		Model.registerObserver(this);
		this.modelChanged();
	}

	/**
	 * Is called if the WHOLE talent tree model changed.
	 */
	public final void modelChanged() {
		this.removeAll();
		this.trees.clear();
		
		ESpec spec = Model.getESpec();
		
		int i = 0;
		for (ETalentTree tt : Specializations.getTalentTreeEnums(spec)) {
			this.trees.add(new TalentTree(tt));
			this.addTab(tt.toString(), this.trees.get(i));
			i++;
		}
		
		this.modelChanged(EModelObserverEvent.SPEC_LOADED);
	}

	@Override
	public final void modelChanged(final EModelObserverEvent e) {
		if (e == EModelObserverEvent.SPEC_LOADED 
				|| e == EModelObserverEvent.TALENT_CHANGED) {
			int index = 0;
			ESpec spec = Model.getESpec();
			
			/* Update the tab titles. */
			for (ETalentTree tree : Specializations.getTalentTreeEnums(spec)) {
				this.setTitleAt(index, tree.toString()
						+ " (" + Model.getProfile().getSpecTemplate()
						.getTreePoints(tree) + ")");
				index++;
			}	
		}
	}
}
